// src/router/index.js
import { createRouter, createWebHistory } from 'vue-router';
import Visitor from '@/views/Visitor.vue';
import Login from '@/views/Login.vue';
import Register from '@/views/Register.vue';
import {ElMessage} from "element-plus";
import { useUserStore } from '@/stores/userStore';
import AdminLayout from '@/views/admin/AdminLayout.vue';
import Dashboard from '@/views/admin/Dashboard.vue';
import Users from '@/views/admin/Users.vue';
import Products from '@/views/admin/Products.vue';
import Orders from '@/views/admin/Orders.vue';
import Categories from '@/views/admin/Categories.vue';

const routes = [
  {
    path: '/',
    name: 'Visitor',
    component: Visitor,
    meta: { requiresAuth: false }
  },
  {
    path: '/login',
    name: 'Login',
    component: Login,
    meta: { requiresAuth: false }
  },
  {
    path: '/register',
    name: 'Register',
    component: Register,
    meta: { requiresAuth: false }
  },
  {
    path: '/products',
    name: 'Products',
    component: () => import('@/views/Products.vue'),
    meta: { requiresAuth: false }
  },
  {
    path: '/products/:id',
    name: 'ProductDetail',
    component: () => import('@/views/ProductDetail.vue'),
    meta: { requiresAuth: false }
  },
  {
    path: '/cart',
    name: 'Cart',
    component: () => import('@/views/Cart.vue'),
    meta: { requiresAuth: true }
  },
  {
    path: '/orders',
    name: 'Orders',
    component: () => import('@/views/Order.vue'),
    meta: { requiresAuth: true }
  },
  {
    path: '/profile',
    name: 'Profile',
    component: () => import('@/views/Profile.vue'),
    meta: { requiresAuth: true },
    children: [
      {
        path: '',
        name: 'UserProfile',
        component: () => import('@/views/Profile.vue'),
        meta: { requiresAuth: true }
      },
      {
        path: 'orders',
        name: 'UserOrders',
        component: () => import('@/views/UserOrders.vue'),
        meta: { requiresAuth: true }
      },
      {
        path: 'address',
        name: 'UserAddress',
        component: () => import('@/views/UserAddress.vue'),
        meta: { requiresAuth: true }
      },
      {
        path: 'balance',
        name: 'UserBalance',
        component: () => import('@/views/UserBalance.vue'),
        meta: { requiresAuth: true }
      },
      {
        path: 'orders/:orderId',
        name: 'UserOrderDetail',
        component: () => import('@/views/UserOrderDetail.vue'),
        meta: { requiresAuth: true }
      }
    ]
  },
  {
    path: '/search',
    name: 'SearchResults',
    component: () => import('@/views/SearchResults.vue'),
    meta: { requiresAuth: false }
  },
  // 卖家端路由体系
  {
    path: '/seller',
    name: 'SellerLayout',
    component: () => import('@/views/seller/Layout.vue'),
    meta: { requiresAuth: true, requiresRole: 'SELLER' },
    redirect: '/seller/dashboard',
    children: [
      {
        path: 'dashboard',
        name: 'SellerDashboard',
        component: () => import('@/views/seller/Dashboard.vue'),
        meta: { requiresAuth: true, requiresRole: 'SELLER' }
      },
      {
        path: 'products',
        name: 'SellerProducts',
        component: () => import('@/views/seller/Products.vue'),
        meta: { requiresAuth: true, requiresRole: 'SELLER' }
      },
      {
        path: 'orders',
        name: 'SellerOrders',
        component: () => import('@/views/seller/Orders.vue'),
        meta: { requiresAuth: true, requiresRole: 'SELLER' }
      },
      {
        path: 'orders/:orderId',
        name: 'SellerOrderDetail',
        component: () => import('@/views/seller/OrderDetail.vue'),
        meta: { requiresAuth: true, requiresRole: 'SELLER' }
      }
    ]
  },
  {
    path: '/admin',
    component: AdminLayout,
    children: [
      { path: '', redirect: 'dashboard' },
      { path: 'dashboard', component: Dashboard },
      { path: 'users', component: Users },
      { path: 'products', component: Products },
      { path: 'orders', component: Orders },
      { path: 'categories', component: Categories }
    ]
  }
];

const router = createRouter({
  history: createWebHistory(),
  routes
});

// 路由守卫
router.beforeEach((to, from, next) => {
  const userStore = useUserStore();
  if (!userStore.isAuthenticated) {
    userStore.initialize();
  }
  
  // 检查是否需要登录
  if (to.meta.requiresAuth && !userStore.isAuthenticated) {
    ElMessage.info('请先登录再访问此页面');
    next({
      name: 'Login',
      query: { redirect: to.fullPath }
    });
    return;
  }
  
  // 检查角色权限
  if (to.meta.requiresRole && userStore.user?.role !== to.meta.requiresRole) {
    ElMessage.error('您没有权限访问此页面');
    next({ name: 'Visitor' });
    return;
  }
  
  next();
});

export default router;